/**
 * 
 */
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Administrator
 *
 */
public class DBHelper {
	
	private static final String driver = "com.mysql.jdbc.Driver";//数据库驱动
	private static final String url = "jdbc:mysql://localhost:3306/novel?useUnicode=true&characterEncoding=UTF-8";
	private static final String username = "root";//数据库用户名
	private static final String password = "root";//数据库密码
	
	private static Connection conn = null;
	
	static{
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() throws SQLException {
		if (conn == null) {
			conn = DriverManager.getConnection(url,username,password);
		}
		return conn;
	}
	
	public static void main(String[] args) {
		try {
			Connection conn = DBHelper.getConnection();
			if (conn != null) {
				System.out.println("数据库连接正常");
			}else {
				System.out.println("数据库连接异常");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
